
cd "YOUR-DIRECTORY"

use z1.MainData.dta, clear
encode mission, gen(mission_numeric)
xtset mission_numeric year, yearly

mkdir FromStata
mkdir Tables


eststo clear


***********
* TABLE 1 *
***********


/* Institutional model */
eststo: nbreg sum_abuses ruleoflawHC pressFreedomHC log_gdppcHC democracyHC ruleoflawTCC pressfreedomTCC democracyTCC log_gdppcTCC sqrt_troops, vce(r)

/* Predictive margins for this model */
foreach var in ruleoflawTCC pressfreedomTCC ruleoflawHC pressFreedomHC {
	local plist
	foreach p of numlist 10(10)90 { /* Make a local list of decile values */
		centile `var' if e(sample), c(`p')
		local plist `plist' `r(c_1)'
	}
	/* Calculate the margins */
	margins, predict(n) atmeans at(`var'=(`plist'))
	/* Grab the estimates and store as variables */
	matrix b1 = r(b)'
	local nam = "margins_`var'"
	svmat b1, names("`nam'")
	matrix b1se = vecdiag(r(V))'
	svmat b1se, names("`nam'_SEs")
	replace `nam'_SEs = sqrt(`nam'_SEs)
	mat drop _all
}
preserve
keep margins_*
drop if margins_ruleoflawTCC1==.
export delimited using "FromStata/margins_model1.csv", replace
restore
drop margins_*



/* Societal model */
eststo: nbreg sum_abuses log_gdppcHC democracyHC maternalTCC giniTCC secondaryTCC log_gdppcTCC democracyTCC sqrt_troops, vce(r)

/* Predictive margins for this model */
foreach var in maternalTCC giniTCC secondaryTCC {
	local plist
	foreach p of numlist 10(10)90 { /* Make a local list of decile values */
		centile `var' if e(sample), c(`p')
		local plist `plist' `r(c_1)'
	}
	/* Calculate the margins */
	margins, predict(n) atmeans at(`var'=(`plist'))
	/* Grab the estimates and store as variables */
	matrix b1 = r(b)'
	local nam = "margins_`var'"
	svmat b1, names("`nam'")
	matrix b1se = vecdiag(r(V))'
	svmat b1se, names("`nam'_SEs")
	replace `nam'_SEs = sqrt(`nam'_SEs)
	mat drop _all
}
preserve
keep margins_*
drop if margins_maternalTCC1==.
export delimited using "FromStata/margins_model2.csv", replace
restore
drop margins_*



/* Military culture model */
eststo: nbreg sum_abuses democracyHC log_gdppcHC IHLlegislationTCC IHLenforcementTCC conscriptionTCC log_gdppcTCC democracyTCC sqrt_troops, vce(r)

/* Predictive margins for this model */
foreach var in IHLlegislationTCC IHLenforcementTCC conscriptionTCC {
	local plist
	foreach p of numlist 10(10)90 { /* Make a local list of decile values */
		centile `var' if e(sample), c(`p')
		local plist `plist' `r(c_1)'
	}
	/* Calculate the margins */
	margins, predict(n) atmeans at(`var'=(`plist'))
	/* Grab the estimates and store as variables */
	matrix b1 = r(b)'
	local nam = "margins_`var'"
	svmat b1, names("`nam'")
	matrix b1se = vecdiag(r(V))'
	svmat b1se, names("`nam'_SEs")
	replace `nam'_SEs = sqrt(`nam'_SEs)
	mat drop _all
}
preserve
keep margins_*
drop if margins_IHLlegislationTCC1==.
export delimited using "FromStata/margins_model3.csv", replace
restore
drop margins_*



/* Send the model estimates to a TeX table */
esttab using "Tables/estimates1.tex", se replace nogaps pr2 aic sca(ll) star(* 0.1 ** 0.05 *** 0.01)
eststo clear



***********
* TABLE 2 *
***********


/* Standard negbin specification with all variables */
eststo: nbreg sum_abuses ruleoflawHC pressFreedomHC log_gdppcHC democracyHC ruleoflawTCC pressfreedomTCC maternalTCC giniTCC secondaryTCC IHLlegislationTCC IHLenforcementTCC conscriptionTCC democracyTCC log_gdppcTCC sqrt_troops, vce(r)

/* PA model */
eststo: xtnbreg sum_abuses ruleoflawHC pressFreedomHC log_gdppcHC democracyHC ruleoflawTCC pressfreedomTCC maternalTCC giniTCC secondaryTCC IHLlegislationTCC IHLenforcementTCC conscriptionTCC democracyTCC log_gdppcTCC sqrt_troops, pa corr(ar1)

/* RE model */
eststo: xtnbreg sum_abuses ruleoflawHC pressFreedomHC log_gdppcHC democracyHC ruleoflawTCC pressfreedomTCC maternalTCC giniTCC secondaryTCC IHLlegislationTCC IHLenforcementTCC conscriptionTCC democracyTCC log_gdppcTCC sqrt_troops, re

/* Export estimates to a TeX table */
esttab using "Tables/estimates2.tex", se replace nogaps pr2 aic sca(ll) star(* 0.1 ** 0.05 *** 0.01)
eststo clear



*************
* FORECASTS *
*************


append using "z2.ForecastData.dta", gen(Forecasts) keep(pressFreedomHC log_gdppcHC ruleoflawHC democracyHC ccode mission year)
drop mission_numeric
encode mission, gen(mission_numeric)

/* Use local macros to specify variables */
local ToUse = "ruleoflawTCC pressfreedomTCC maternalTCC giniTCC secondaryTCC IHLlegislationTCC IHLenforcementTCC conscriptionTCC democracyTCC log_gdppcTCC sqrt_troops"
local Model = "sum_abuses ruleoflawHC pressFreedomHC log_gdppcHC democracyHC ruleoflawTCC pressfreedomTCC maternalTCC giniTCC secondaryTCC IHLlegislationTCC IHLenforcementTCC conscriptionTCC democracyTCC log_gdppcTCC sqrt_troops"

/* "Most likely case." Use mean values for all TCC measures. */
foreach var of varlist `ToUse' {
	quietly sum `var' if Forecasts!=1 /* Sum only over "real" observations */
	display `r(mean)'
	replace `var' = `r(mean)' if Forecasts==1
}
nbreg `Model' if Forecasts!=1, vce(r)
predictnl PredictAvg=predict() if Forecasts==1
foreach var of varlist `ToUse' {
	replace `var' = . if Forecasts==1
}

/* "Worst case." 2006 Congo mission (MONUC) had the most violations, so use values from that. */
foreach var of varlist `ToUse' {
	quietly sum `var' if mission=="MONUC" & year==2006 & Forecasts!=1
	display `r(mean)'
	replace `var' = `r(mean)' if Forecasts==1
}
quietly nbreg `Model' if Forecasts!=1, vce(r)
predictnl PredictWorstCONGO=predict() if Forecasts==1
foreach var of varlist `ToUse' {
	replace `var' = . if Forecasts==1
}

/* "Best case." Use values from 2005 UNOMIG (Georgia) mission. */
foreach var of varlist `ToUse' {
	quietly sum `var' if mission=="UNOMIG" & year==2005 & Forecasts!=1
	display `r(mean)'
	replace `var' = `r(mean)' if Forecasts==1
}
quietly nbreg `Model' if Forecasts!=1, vce(r)
predictnl PredictBestUNOMIG=predict() if Forecasts==1
foreach var of varlist `ToUse' {
	replace `var' = . if Forecasts==1
}

/* Send the forecasts to a .csv file */
preserve
quietly sum ruleoflawHC
replace ruleoflawHC = ruleoflawHC + abs(`r(min)')
keep if Forecasts==1
keep year mission Predict* ruleoflawHC pressFreedomHC
export delimited using "FromStata/Forecasts.csv", replace
restore
drop Predict*
clear
